package com.xiaorun.portal.controller.admin;

import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.controller.BaseController;
import com.xiaorun.portal.exception.TipException;
import com.xiaorun.portal.modal.Bo.RestResponseBo;
import com.xiaorun.portal.modal.Vo.News;
import com.xiaorun.portal.service.INewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 13 on 2018/2/21.
 */
@Controller()
@RequestMapping("admin/news")
@Slf4j
public class NewsController extends BaseController {

    @Autowired
    private INewsService newsService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody(required = false) Map<String, Object> params, HttpServletRequest request) {
        return this.list(1, 10, params, request);
    }

    @RequestMapping(value = "/list/{pageNum}/{pageSize}")
    public String list(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody(required = false) Map<String, Object> params, HttpServletRequest request) {
        PageInfo<News> page = newsService.list(pageNum, pageSize, params);
        request.setAttribute("news", page);
        return "admin/news_list";
    }

    @RequestMapping(value = "/search")
    public String search(String title, String applyStatus, String projectLevel,String fundAmount, String fundType,String fundMode,HttpServletRequest request) {
        Map<String, Object>  params = new HashMap<>(1);
        params.put("title", title);
        params.put("applyStatus", applyStatus);
        params.put("projectLevel", projectLevel);
        params.put("fundAmount", fundAmount);
        params.put("fundType", fundType);
        params.put("fundMode", fundMode);
        return this.list(1, 10, params, request);
    }

    @GetMapping(value = "/add")
    public String add(HttpServletRequest request) {
        return "admin/news_edit";
    }

    @GetMapping(value = "/edit/{id}")
    public String editPage(@PathVariable Integer id, HttpServletRequest request) {
        News news = newsService.get(id);
        request.setAttribute("news", news);
        return "admin/news_edit";
    }

    @PostMapping(value = "/save")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo save(News news, HttpServletRequest request) {
        try {
            news.setAuthorId(this.getUid(request));
            newsService.save(news);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "添加新闻失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                log.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }

        return RestResponseBo.ok();
    }

    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@PathVariable int id, HttpServletRequest request) {
        try {
            newsService.remove(id);
        } catch (Exception e) {
            String msg = "删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                log.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }

}
